import { Injectable, Optional, Inject } from '@nestjs/common';

// 定义配置接口
interface ConfigOptions {
  apiKey?: string;
  timeout?: number;
  baseUrl?: string;
}

// 默认配置
const DEFAULT_CONFIG: ConfigOptions = {
  apiKey: 'default-api-key',
  timeout: 5000,
  baseUrl: 'https://api.example.com',
};

@Injectable()
export class ConfigService {
  private readonly config: ConfigOptions;

  constructor(@Optional() @Inject('CONFIG_OPTIONS') config?: ConfigOptions) {
    // 如果未提供配置，则使用默认配置
    this.config = { ...DEFAULT_CONFIG, ...config };
  }

  // 获取配置值的方法
  getApiKey(): string {
    return this.config.apiKey;
  }

  getTimeout(): number {
    return this.config.timeout;
  }

  getBaseUrl(): string {
    return this.config.baseUrl;
  }

  // 获取完整配置
  getConfig(): ConfigOptions {
    return { ...this.config };
  }
}